package demo.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;


import com.alibaba.fastjson.JSONObject;

/**
 * 类型转换工具类
 * 
 * @author hanjy
 *
 */
public class TransferUtils {

    private TransferUtils() {
        super();
    }

    /**
     * 将MAP VALUE的OBJECT 类型转String类型
     * 
     * @param map
     * @return
     */
    public static Map<String, String> transfer(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return Collections.emptyMap();
        }
        return map.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, t -> object2JsonString(t.getValue())));
    }

    public static String object2JsonString(Object o) {
        if (o == null) {
            return "null";
        }
        if (String.class.isInstance(o)) {
            return o.toString();
        }
        return JSONObject.toJSONString(o);
    }
}
